<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>
<body>
    <!-- 导航栏 -->
    <div class="nav">
        <div class="logo-container">
            <img src="pic/Logo.png" class="nav-logo">
        </div>
        <ul class="nav-list">
            <li><a href="#">聊天室</a></li>
            <li><a href="#">魔法相机</a></li>
            <li><a href="#">关于</a></li>
            <li><a href="#">联系</a></li>
        </ul>
    </div>

     <!--主窗口区-->
     <div class="container">
        <div class="login-box">
            <div class="login-pic"></div>
            <div class="login-dialog">
                <h1>登录</h1>
                <input type="text" class="inputItem" placeholder="用户名" id="username">
                <input type="password" class="inputItem" placeholder="密码" id="password">
                <a href="register.html" class="register">立即注册</a>
                <div class="row">
                    <button class = "btn" id="submit">登录</button>
                </div>
            </div>
        </div>
     </div>

    <!--气泡浮动效果-->   
        <div class="bubbles">
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
            <div class="bubble"></div>
        </div>

    <!--引入jQuery-->
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <!--实现前后端交互-->
    <script>
        let submitButton = document.querySelector('#submit');
        submitButton.onclick = function() {
            //1. 先获取到输入框的内容
            let username = document.querySelector('#username').value;
            let password = document.querySelector('#password').value;
            if(username == '' || password == '') {
                alert("当前输入的用户名或密码为空！");
                return;
            }

            //2. 再发送 ajax 请求进行登录
            $.ajax({
                url: '/login',
                type: 'post',
                data: {
                    username: username,
                    password: password
                },
                success: function(response) {
                    //3. 处理响应
                    if(response && response.userId > 0) {
                        //登录成功
                        alert('登录成功！');
                        location.assign('/client.html');
                    } else {
                        if (response.error === "USER_NOT_FOUND") {
                            alert('用户名不存在');
                            document.querySelector('#username').focus(); // 聚焦到用户名输入框
                        }
                        else if (response.error === "WRONG_PASSWORD") {
                            alert('密码错误');
                            document.querySelector('#password').value = ""; // 清空密码框
                            document.querySelector('#password').focus(); // 聚焦到密码输入框
                        }
                    }
                }
            })
        }
    </script>
</body>
</html>